package com.snowfeel.sfmall.order.auth.service;

import com.snowfeel.sfmall.api.sys.feign.SysUserFeignClient;
import com.snowfeel.sfmall.api.sys.feign.dto.LoginUserDto;
import com.snowfeel.sfmall.core.response.JsonResult;
import com.snowfeel.sfmall.order.auth.convert.SecurityUserConvert;
import com.snowfeel.sfmall.security.vo.SecurityUserVo;
import com.snowfeel.sfmall.security.vo.SysUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author guomenghuai
 * @date 2024/9/8
 * @Description
 */
@Service
public class OmsUserDetailService implements UserDetailsService {

    @Autowired
    private SysUserFeignClient userFeignClient;

    @Autowired
    private SecurityUserConvert securityUserConvert;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        JsonResult<LoginUserDto> jsonResult = userFeignClient.getUserInfo(username);
        if (Objects.isNull(jsonResult) || !jsonResult.isSuccess()) {
            return null;
        }
        LoginUserDto data = jsonResult.getData();
        SecurityUserVo securityUserVo = securityUserConvert.convertVo(data.getUser());
        return new SysUserDetails(securityUserVo, data.getPermissionList());
    }
}
